package com.dankegongyu.lib.http;

import com.dankegongyu.lib.http.request.DeleteRequest;
import com.dankegongyu.lib.http.request.GetRequest;
import com.dankegongyu.lib.http.request.PostRequest;
import com.dankegongyu.lib.http.request.PutRequest;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.internal.platform.Platform;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * 网络请求入口类
 * 主要功能：
 * 1.全局设置超时时间
 * 2.支持GET、post、delete、put请求
 * 3.支持自定义请求
 * 4.支持全局公共请求头
 * 5.支持全局公共参数
 * 6.支持OkHttp相关参数，包括拦截器
 *
 * @author wupuquan
 * @version 1.0
 * @since 2018/6/12 14:33
 */
@SuppressWarnings("UnusedReturnValue")
public final class DKHttp {

    private static boolean isDebug = false;

    /**
     * debug模式
     */
    public static void setDebugMode(boolean debug) {
        isDebug = debug;
    }

    public static boolean isDebugMode() {
        return isDebug;
    }

    public static class Builder {

        // 默认超时时间
        private static final int DEFAULT_TIMEOUT = 15_000;

        private OkHttpClient.Builder okHttpClientBuilder;

        private Builder() {
            okHttpClientBuilder = new OkHttpClient.Builder();
            okHttpClientBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
            okHttpClientBuilder.readTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
            okHttpClientBuilder.writeTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);

            if (DKHttp.isDebugMode()) {
                okHttpClientBuilder.addInterceptor(new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                    @Override
                    public void log(@SuppressWarnings("NullableProblems") String message) {
                        // Logger默认log级别是INFO，有的华为测试机打不出来，改为WARN可以了
                        Platform.get().log(Platform.WARN, message, null);
                    }
                }).setLevel(HttpLoggingInterceptor.Level.BODY));
            }
        }

        public static Builder newInstance() {
            return new Builder();
        }

        /**
         * 设置连接超时时间
         */
        public Builder setTimeout(long timeout) {
            setConnectTimeout(timeout);
            setReadTimeout(timeout);
            setWriteTimeout(timeout);
            return this;
        }

        /**
         * 设置连接超时时间
         */
        public Builder setConnectTimeout(long connectTimeout) {
            if (connectTimeout > 0) {
                okHttpClientBuilder.connectTimeout(connectTimeout, TimeUnit.MILLISECONDS);
            }
            return this;
        }

        /**
         * 设置读操作超时时间
         */
        public Builder setReadTimeout(long readTimeout) {
            if (readTimeout > 0) {
                okHttpClientBuilder.readTimeout(readTimeout, TimeUnit.MILLISECONDS);
            }
            return this;
        }

        /**
         * 设置写操作超时时间
         */
        public Builder setWriteTimeout(long writeTimeout) {
            if (writeTimeout > 0) {
                okHttpClientBuilder.writeTimeout(writeTimeout, TimeUnit.MILLISECONDS);
            }
            return this;
        }

        /**
         * 设置缓存路径和大小
         *
         * @param cacheFile 缓存文件，如：getExternalCacheDir().toString()，即：SDCard/Android/data/当前应用包名/cache/
         * @param cacheSize 缓存大小，单位字节，如：10M = 10 * 1024 * 1024B
         * @return DKHttp
         */
        public Builder setCache(File cacheFile, int cacheSize) {
            if (cacheFile != null && cacheSize > 0) {
                okHttpClientBuilder.cache(new Cache(cacheFile, cacheSize));
            }
            return this;
        }

        public Builder addInterceptor(Interceptor interceptor) {
            if (interceptor != null) {
                okHttpClientBuilder.addInterceptor(interceptor);
            }
            return this;
        }

        OkHttpClient build() {
            return okHttpClientBuilder.build();
        }
    }

    /**
     * GET请求
     *
     * @param url     完整url
     * @param builder 自定义网络请求配置
     */
    public static GetRequest get(String url, Builder builder) {
        return new GetRequest(url, builder.build());
    }

    /**
     * POST请求
     *
     * @param url     完整url
     * @param builder 自定义网络请求配置
     */
    public static PostRequest post(String url, Builder builder) {
        return new PostRequest(url, builder.build());
    }

    /**
     * PUT请求
     *
     * @param url     完整url
     * @param builder 自定义网络请求配置
     */
    public static PutRequest put(String url, Builder builder) {
        return new PutRequest(url, builder.build());
    }

    /**
     * DELETE请求
     *
     * @param url     完整url
     * @param builder 自定义网络请求配置
     */
    public static DeleteRequest delete(String url, Builder builder) {
        return new DeleteRequest(url, builder.build());
    }


    public static GetRequest get(String url) {
        return get(url, Builder.newInstance());
    }

    public static PostRequest post(String url) {
        return post(url, Builder.newInstance());
    }

    public static PutRequest put(String url) {
        return put(url, Builder.newInstance());
    }

    public static DeleteRequest delete(String url) {
        return delete(url, Builder.newInstance());
    }
}
